package test.adddata;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import entity.Pubmed;
import lombok.SneakyThrows;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import utils.PubmedUtils2;
import utils.SmallTool;
import utils.eutilsUtil.http.Eutils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: gcr
 * @Date: 2022-3-30 15:46
 */
public class GetXmlInsertDB {

    public static String localPm = "spider_pubmed_vip_extend";
    //    static String dbName2 = "xml_content_3qw";
    static String dbName2 = "xml_content_1_3qw";
    // 全部插入不判断 如果是true 则进行全部插入
    static boolean insert = false;
    //是不是要更新
    static boolean isupdate = false;
    //通过id进行开始


    @SneakyThrows
    public static void main(String[] args) {
        //  2021年11月18日15:41:15 插入2800万以后的数据,进行查询 没有的才插入,有就先不更新了
        //先删除 后插入

//        DSFactory.setCurrentDSFactory(new DruidDSFactory());
//        DataSource ds = DSFactory.get("server_local");
//        DataSource taiyids = DSFactory.get("taiyi");
        int startPmid = 37400000;
        String pmXml = Eutils.getPmXml(startPmid + "");
        Db usetaiyi = Db.use(DSFactory.get("local_articleadd"));
        //解析html
        Document doc = Jsoup.parse(pmXml);
        try {
            // 主要是用于进行过滤掉参文房子参文doi混入文章doi
            doc.select("ReferenceList").remove();
        } catch (Exception e) {

        }
        List<Element> divList = new ArrayList<>();
        Elements pubmedArticles = doc.select("PubmedArticle");
        Elements pubmedBookArticles = doc.select("PubmedBookArticle");
        for (Element e : pubmedArticles) {
            divList.add(e);
        }
        for (Element e : pubmedBookArticles) {
            divList.add(e);
        }
        List<Pubmed> pubmeds = PubmedUtils2.parseHtml(divList);
        //直接入库
        for (Pubmed pubmed : pubmeds) {
            if (pubmed.getPmid() <= startPmid) {
                SmallTool.printMess("小于开始 略过");
                continue;
            } else {
                try {
                    insertData(pubmed, usetaiyi);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @SneakyThrows
    public static void insertData(Pubmed pubmed, Db use) {
        if (insert) {
            use.insert(new Entity().setTableName(localPm)
                    .set("pmid", pubmed.getPmid())
                    .set("type", pubmed.getType())
                    .set("article_title", pubmed.getArticleTitle())
                    .set("source", pubmed.getSource())
                    .set("intro", pubmed.getIntro())
                    .set("jour", pubmed.getJour())
                    .set("volume", pubmed.getVolume())
                    .set("issue", pubmed.getIssue())
                    .set("pages", pubmed.getPages())
                    .set("vip", pubmed.getVip())
                    .set("pub_date", pubmed.getPubDate())
                    .set("medline_date", pubmed.getMedlineDate())
                    .set("pub_year", pubmed.getPubYear())
                    .set("med_nlm_id", pubmed.getMedNlmId())
                    .set("author", pubmed.getAuthor())
                    .set("author_extend", pubmed.getAuthorExtend())
                    .set("affs", pubmed.getAffs())
                    .set("pmcid", pubmed.getPmcid())
                    .set("doi", pubmed.getDoi())
                    .set("lan", pubmed.getLan())
                    .set("ab_text", pubmed.getAbText())
                    .set("copyright", pubmed.getCopyright())
                    .set("keyword", pubmed.getKeyword())
                    .set("pts", pubmed.getPts())
                    .set("meshs", pubmed.getMeshs())
                    .set("mesh_heading", pubmed.getMeshHeading())
                    .set("mesh_headingy", pubmed.getMeshHeadingy())
                    .set("subs", pubmed.getSubs())
                    .set("grants", pubmed.getGrants())
                    .set("ass_date", pubmed.getAssDate())
                    .set("full_text", pubmed.getFullText())
                    .set("art_type", pubmed.getArtType())
            );
        } else {
            Entity info = null;
            if (info == null) {
                try {
                    use.insert(new Entity().setTableName(localPm)
                            .set("pmid", pubmed.getPmid())
                            .set("type", pubmed.getType())
                            .set("article_title", pubmed.getArticleTitle())
                            .set("source", pubmed.getSource())
                            .set("intro", pubmed.getIntro())
                            .set("jour", pubmed.getJour())
                            .set("volume", pubmed.getVolume())
                            .set("issue", pubmed.getIssue())
                            .set("pages", pubmed.getPages())
                            .set("vip", pubmed.getVip())
                            .set("pub_date", pubmed.getPubDate())
                            .set("medline_date", pubmed.getMedlineDate())
                            .set("pub_year", pubmed.getPubYear())
                            .set("med_nlm_id", pubmed.getMedNlmId())
                            .set("author", pubmed.getAuthor())
                            .set("author_extend", pubmed.getAuthorExtend())
                            .set("affs", pubmed.getAffs())
                            .set("pmcid", pubmed.getPmcid())
                            .set("doi", pubmed.getDoi())
                            .set("lan", pubmed.getLan())
                            .set("ab_text", pubmed.getAbText())
                            .set("copyright", pubmed.getCopyright())
                            .set("keyword", pubmed.getKeyword())
                            .set("pts", pubmed.getPts())
                            .set("meshs", pubmed.getMeshs())
                            .set("mesh_heading", pubmed.getMeshHeading())
                            .set("mesh_headingy", pubmed.getMeshHeadingy())
                            .set("subs", pubmed.getSubs())
                            .set("grants", pubmed.getGrants())
                            .set("ass_date", pubmed.getAssDate())
                            .set("full_text", pubmed.getFullText())
                            .set("art_type", pubmed.getArtType())
                    );
                } catch (Exception e) {
                    e.printStackTrace();
                }

            } else {

                if (isupdate) {
                    Entity delE = new Entity();
                    delE.setTableName(localPm);
                    delE.set("pmid", pubmed.getPmid());
                    Entity entity1 = new Entity();
                    entity1.set("pmid", pubmed.getPmid())
                            .set("type", pubmed.getType())
                            .set("article_title", pubmed.getArticleTitle())
                            .set("source", pubmed.getSource())
                            .set("intro", pubmed.getIntro())
                            .set("jour", pubmed.getJour())
                            .set("volume", pubmed.getVolume())
                            .set("issue", pubmed.getIssue())
                            .set("pages", pubmed.getPages())
                            .set("vip", pubmed.getVip())
                            .set("pub_date", pubmed.getPubDate())
                            .set("medline_date", pubmed.getMedlineDate())
                            .set("pub_year", pubmed.getPubYear())
                            .set("med_nlm_id", pubmed.getMedNlmId())
                            .set("author", pubmed.getAuthor())
                            .set("author_extend", pubmed.getAuthorExtend())
                            .set("affs", pubmed.getAffs())
                            .set("pmcid", pubmed.getPmcid())
                            .set("doi", pubmed.getDoi())
                            .set("lan", pubmed.getLan())
                            .set("ab_text", pubmed.getAbText())
                            .set("copyright", pubmed.getCopyright())
                            .set("keyword", pubmed.getKeyword())
                            .set("pts", pubmed.getPts())
                            .set("meshs", pubmed.getMeshs())
                            .set("mesh_heading", pubmed.getMeshHeading())
                            .set("mesh_headingy", pubmed.getMeshHeadingy())
                            .set("subs", pubmed.getSubs())
                            .set("grants", pubmed.getGrants())
                            .set("ass_date", pubmed.getAssDate())
                            .set("full_text", pubmed.getFullText())
                            .set("art_type", pubmed.getArtType());
                    use.update(entity1, delE);
                }

            }
        }
    }

    @SneakyThrows
    public static Entity getInfo(Db use, Integer pmid) {
        String sqlParam = "select pmid from " + localPm + " where pmid = ?";
        Entity entity = use.queryOne(sqlParam, pmid);
        return entity;
    }

}
